<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page="page" :permission="permissionList"
               :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave"
               @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
      <!-- <template slot="menuLeft">
          <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.bj_delete"
              @click="handleDelete">删 除
          </el-button>
      </template> -->
      <!-- <template slot-scope="{type,size,row,index}" slot="menuBtn">
          <el-dropdown-item divided @click.native="showJobRecords(row, index)">上传文件</el-dropdown-item>
      </template> -->
    </avue-crud>
    <el-dialog title="上传文件" :visible.sync="dialogVisible" width="30%" center :before-close="handleClose">
      <avue-form :option="upOption" v-model="upform">
      </avue-form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitrep">确 定</el-button>
            </span>
    </el-dialog>
  </basic-container>
</template>

<script>
import {getList, add, update, remove, jsList, GetFileUploadInfo} from "@/api/service/jwhy";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      dialogVisible: false,
      isThrottle: false,
      typeRdeio: 1,
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 7,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        labelWidth: 100,
        // menuType: 'menu',
        // menuBtnTitle: '更多操作',
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学校名称",
            prop: "schoolId",
            search: false,
            rules: [{
              required: true,
              message: "请输入学校名称",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
            cascader: ['ParticipantId'],
            props: {
              label: "xxName",
              value: "id"
            },
            change: ({value}) => {
              this.form.schoolId = value
              // this.form.ParticipantId = ''
              this.getTeacherData();
            }
          },
          {
            label: "会议主题",
            prop: "name",
            maxlength: 20,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入会议主题",
              trigger: "blur"
            }],
          },
          {
            label: "开始时间",
            prop: "startTime",
            search: false,
            type: "datetime",
            format: "yyyy-MM-dd HH:mm",
            valueFormat: "yyyy-MM-dd HH:mm",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择开始时间",
              trigger: "blur"
            }]
          },
          {
            label: "会议类型",
            prop: "mode",
            type: 'radio',
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择会议类型",
              trigger: "blur"
            }],
            value: 1,
            hide: true,
            dicData: [{
              label: '全体会议',
              value: 1
            }, {
              label: '专题会议',
              value: 2
            }],
            change: (e) => {
              this.participant(e)
            },
          },
          {
            label: "教师",
            prop: "ParticipantId",
            overHidden: true,
            rules: [{
              required: false,
              message: "请选择教师",
              trigger: "blur"
            }],
            hide: true,
            display: false,
            type: 'select',
            dicData: [],
            multiple: true,
            props: {
              label: "name",
              value: "id"
            },
          },
          // {
          //     label: "人数",
          //     prop: "leverage",
          //     overHidden: true,
          //     rules: [{
          //         required: true,
          //         message: "请输入人数",
          //         trigger: "blur"
          //     }],
          // },
          {
            label: "会议时长（分钟）",
            prop: "hour",
            overHidden: true,
            type: 'number',
            rules: [{
              required: true,
              message: "请输入会议时长",
              trigger: "blur"
            }],
          },
          {
            label: "会议描述",
            prop: "description",
            type: "textarea",
            span: 24,
            maxlength: 800,
            showWordLimit: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入会议描述",
              trigger: "blur"
            }]
          },
        ]
      },
      upform: [],
      upOption: {
        labelWidth: 120,
        column: [
          {
            label: '数组对象',
            prop: 'array',
            dataType: 'object',
            type: 'upload',
            propsHttp: {
              res: 'data'
            },
            span: 24,
            action: ''
          },]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.bj_add, false),
        viewBtn: this.vaildData(this.permission.bj_view, false),
        delBtn: this.vaildData(this.permission.bj_delete, false),
        editBtn: false
      };
    },
    schoolId() {
      return this.JSuserInfo.schoolId
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    getTeacherData() {
      if (this.form.schoolId === '' || this.form.schoolId === null || this.form.schoolId === undefined) return 0
      let ParticipantIds = this.findObject(this.option.column, "ParticipantId");
      let id = this.form.schoolId
      jsList(7, id).then(res => {
        ParticipantIds.dicData = res.data.data;
      })
    },
    participant(data) {
      if (this.isThrottle) {
        return;
      }
      this.isThrottle = true
      let ParticipantIds = this.findObject(this.option.column, "ParticipantId");
      let type = data.value || 1
      let id = this.form.schoolId
      //types  类别 2学校,3年级,4班级 7教师 8学生
      if (type == 2) {
        jsList(7, id).then(res => {
          ParticipantIds.display = true
          ParticipantIds.dicData = res.data.data;
          this.isThrottle = false
        })
      } else {
        // this.form.ParticipantId = '0'
        ParticipantIds.display = false
        this.isThrottle = false
      }
    },
    //上传文件
    showJobRecords(row, index) {
      console.log(index)
      this.dialogVisible = true
      GetFileUploadInfo(row.id).then(res => {
        console.log(res.data.data)
        let array = this.findObject(this.upOption.column, "array");
        array.action = res.data.data.url
      })
    },
    rowSave(row, done, loading) {
      row.id = '0'
      // row.schoolId = this.schoolId
      // if (row.jSDtos.length > 0) {
      //     row.ParticipantId = row.jSDtos.toString()
      // } else {
      //     row.ParticipantId = row.participantName
      // }
      row.ParticipantId = row.ParticipantId.length >= 0 ? '0' : row.ParticipantId.toString()
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({id: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({id: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {

        // getDetail(this.form.id).then(res => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      // this.query.schoolId = this.schoolId
      // console.log(this.schoolId)
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        let newParticipantId = []
        if (data.total > 0) {
          data.records.forEach(item => {
            newParticipantId = []
            if (item.releaseDtos.length > 0) {
              item.releaseDtos.forEach(items=>{
                newParticipantId.push(items.id)
              })
            }
            console.log(newParticipantId)
            item.ParticipantId = newParticipantId
          })
        }
        this.data = data.records;
        console.log(this.data)
        this.loading = false;
        this.selectionClear();
      });
    }

  }
};
</script>

<style></style>
